using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Pitoshka.Tools.ResourceFilesChecker
{
	class ResDirectory
	{
		private string _ResDirectoryPath;
		public string ResDirectoryPath
		{
			get { return _ResDirectoryPath; }
			set { _ResDirectoryPath = value; }
		}

		private List<ResDirectory> _Children = new List<ResDirectory>();
		public List<ResDirectory> Children
		{
			get { return _Children; }
			set { _Children = value; }
		}

		private List<ResourceFile> _ResourceFiles = new List<ResourceFile>();
		public List<ResourceFile> ResourceFiles
		{
			get { return _ResourceFiles; }
			set { _ResourceFiles = value; }
		}

		public ResDirectory(string path)
		{
			this.ResDirectoryPath = path;
		}

		public void Load()
		{
			// Load directories
			this.Children.Clear();
			string[] children = Directory.GetDirectories(this.ResDirectoryPath, "*", SearchOption.TopDirectoryOnly);
			foreach (string child in children)
			{
				if (!((File.GetAttributes(child) & FileAttributes.Hidden) == FileAttributes.Hidden))
				{
					this.Children.Add(new ResDirectory(child));
				}
			}
			foreach (ResDirectory dir in this.Children)
			{
				dir.Load();				
			}


			// Load resource files
			this.ResourceFiles.Clear();

			foreach (string ext in Program.AllowedExtensions)
			{
				string[] files = Directory.GetFiles(this.ResDirectoryPath, "*." + ext + ".resx", SearchOption.TopDirectoryOnly);
				foreach (string file in files)
				{
					this.ResourceFiles.Add(new ResourceFile(file));
				}
			}
		}

		public void Check()
		{
			foreach (ResDirectory dir in this.Children)
			{
				dir.Check();
			}
			foreach (ResourceFile file in this.ResourceFiles)
			{
				file.Check();
			}
		}

		public void FixErrors()
		{
			foreach (ResDirectory dir in this.Children)
			{
				dir.FixErrors();
			}
			foreach (ResourceFile file in this.ResourceFiles)
			{
				file.FixErrors();
			}
		}
	}
}
